﻿using LS.DicomViewer.ViewModels;
using System;
using System.Windows;
using System.Windows.Controls;

namespace LS.DicomViewer
{
    /// <summary>
    /// ImageThumbnailView.xaml 的交互逻辑
    /// </summary>
    public partial class ImageThumbnailView : UserControl
    {
        public ImageThumbnailViewModel ViewModel => this.mainGrid.DataContext as ImageThumbnailViewModel;

        public static readonly DependencyProperty ImageFileProperty = DependencyProperty.Register("ImageFile", typeof(string), typeof(ImageThumbnailView), new PropertyMetadata(string.Empty, (a, e) =>
        {
            var vm = ((ImageThumbnailView)a).ViewModel;
            if (e.NewValue == null)
            {
                vm.Clear();
            }
            else
            {
                try
                {
                    vm.Load(e.NewValue.ToString());
                }
                catch (Exception)
                {
                }
            }
        }));

        public static readonly DependencyProperty DoubleClickShowPreviewProperty = DependencyProperty.Register("DoubleClickShowPreview", typeof(bool), typeof(ImageThumbnailView), new PropertyMetadata(true));

        /// <summary>
        /// 双击时是否显示文件预览
        /// </summary>
        public bool DoubleClickShowPreview
        {
            get => (bool)GetValue(DoubleClickShowPreviewProperty);
            set => SetValue(DoubleClickShowPreviewProperty, value);
        }

        public Action ShowViewerAction { get; set; }

        /// <summary>
        /// Dicom文件
        /// </summary>
        public string ImageFile { get => (string)this.GetValue(ImageFileProperty); set => this.SetValue(ImageFileProperty, value); }

        public ImageThumbnailView()
        {
            InitializeComponent();
        }

        private void Image_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2 && DoubleClickShowPreview)
            {
                if (ShowViewerAction != null)
                {
                    ShowViewerAction.Invoke();
                }
                else
                {
                    ViewModel.ShowImageViewerCommand.Execute();
                }
            }
        }
    }
}